package com.soten.gpsdemo.manager;

import android.content.Context;
import android.location.LocationManager;
import android.os.PowerManager;
import android.preference.CheckBoxPreference;
import android.provider.Settings;
import android.util.Log;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class GPSManager
{
    public static final String TAG = "GPSManager";
    public static final String GPS = "GPS";
    public static final String BEIDOU = "BEIDOU";
    public static final String UART2_POWER = "uart2_power";


    private Context mContext;
    private LocationManager mLocationManager;
    private PowerManager mPowerManager;
    private CheckBoxPreference mCbp;
    private boolean isGps;
    private boolean isNetWork;

    public GPSManager(Context context)
    {
        //位置管理者
        mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        //电源管理者
        mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        this.mContext = context;

        isGps = isGPSEnable();
    }

    /*
     * 获取GPS管理者(LocationManager)
     * */
    public LocationManager getLocationManager()
    {
        return mLocationManager;
    }

    /*
     * 是否打开GPS
     * */
    public boolean isGPSEnable()
    {
        return mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }

    /*
     * 开启GPS
     * */
    public void startGPS()
    {
        //设置-安全-查看GPS是否已经打开
        boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(mContext.getContentResolver(), LocationManager.GPS_PROVIDER);
        if (!gpsEnabled)//判断GPS是否已经启动，如果没启动，则运行下面代码
        {
            // 启动GPS
            Settings.Secure.setLocationProviderEnabled(mContext.getContentResolver(), LocationManager.GPS_PROVIDER, true);
            //  Settings.Secure.setLocationProviderEnabled(mContext.getContentResolver(), LocationManager.NETWORK_PROVIDER, true);
            Log.d(TAG, "GPS启动：" + isGPSEnable());
        }
        isGps = isGPSEnable();

    }

    /*
     * 停止GPS
     * */
    public void stopGPS()
    {
        boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(mContext.getContentResolver(), LocationManager.GPS_PROVIDER);
        if (!isGps && gpsEnabled)
        {
            // 关闭GPS
            Settings.Secure.setLocationProviderEnabled(mContext.getContentResolver(), LocationManager.GPS_PROVIDER, false);
            //         Settings.Secure.setLocationProviderEnabled(mContext.getContentResolver(), LocationManager.NETWORK_PROVIDER, false);
            Log.d(TAG, "GPS关闭：" + isGPSEnable());
        }
    }

    /*
     * 开启北斗
     * */
    public void startBeiDou()
    {
        try
        {
            Class<?> forName = Class.forName("android.os.PowerManager");

            Method method = forName.getMethod("uartPowerOpen", int.class, boolean.class);
            if (method != null)
            {
                method.setAccessible(false);//false-->检查
                method.invoke(mPowerManager, 2, true);//调用
                boolean b = Settings.System.putInt(mContext.getContentResolver(), UART2_POWER, 1);
                Log.d(TAG, "北斗启动：" + b);
            }
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (NoSuchMethodException e)
        {
            e.printStackTrace();
        }
        catch (IllegalArgumentException e)
        {
            e.printStackTrace();
        }
        catch (IllegalAccessException e)
        {
            e.printStackTrace();
        }
        catch (InvocationTargetException e)
        {
            e.printStackTrace();
        }
    }

    /*
     * 关闭北斗
     * */
    public void stopBeiDou()
    {
        try
        {
            Class<?> forName = Class.forName("android.os.PowerManager");

            Method method = forName.getMethod("uartPowerOpen", int.class,
                    boolean.class);
            if (method != null)
            {
                method.setAccessible(false);
                //调用uartPowerOpen(int,boolean)方法，第一个参数为该类的实例
                method.invoke(mPowerManager, 2, false);
                //把值写入数据库，UART2_POWER为键，0为值
                boolean b = Settings.System.putInt(mContext.getContentResolver(), UART2_POWER, 0);
                Log.d(TAG, "北斗关闭：" + b);
            }
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (NoSuchMethodException e)
        {
            e.printStackTrace();
        }
        catch (IllegalArgumentException e)
        {
            e.printStackTrace();
        }
        catch (IllegalAccessException e)
        {
            e.printStackTrace();
        }
        catch (InvocationTargetException e)
        {
            e.printStackTrace();
        }
    }
}
